package com.socct.test2


import android.os.Bundle
import android.util.SparseArray
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.activity.addCallback
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2

/**
 * A simple [Fragment] subclass.
 */
class TestFragment9 : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_test_fragment9, container, false)
        initView(view)
        return view
    }

    private fun initView(view: View?) {
        view?.findViewById<ViewPager2>(R.id.vp2)?.run {
            this.orientation = ViewPager2.ORIENTATION_VERTICAL
            adapter = MyAdapter(this@TestFragment9)
        }

        val back = requireActivity().onBackPressedDispatcher.addCallback(this) {

        }.run {
            isEnabled = true
        }
      

    }


}


class MyAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
    private val fragments = SparseArray<Fragment>()

    override fun getItemCount(): Int {
        return 20;
    }

    override fun createFragment(position: Int): Fragment {
        var get = fragments.get(position)
        if (get == null) {
            get = TestFragment3()
            fragments.put(position, get)
        }
        return get
    }

}
